package com.quanyan.financialSystem.service.place.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.financialSystem.entity.db.TbBizOrderMemberReconciliation;
import com.quanyan.financialSystem.entity.db.TbBizOrderMemberReconciliationExample;
import com.quanyan.financialSystem.entity.vo.place.req.ReqMemberReconciliation;
import com.quanyan.financialSystem.entity.vo.place.req.ReqMemberReconciliationUpdate;
import com.quanyan.financialSystem.entity.vo.place.resp.MemberReconciliationVo;
import com.quanyan.financialSystem.entity.vo.place.resp.MemberReconciliationVos;
import com.quanyan.financialSystem.mapper.MemberReconciliationMapper;
import com.quanyan.financialSystem.mapper.TbBizOrderMemberReconciliationMapper;
import com.quanyan.financialSystem.service.place.MemberReconciliationService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * OrderReconciliationServiceImpl
 *
 * @author Tu Chun
 * @date 2016/10/24
 */
@Service("memberReconciliationService")
public class MemberReconciliationServiceImpl implements MemberReconciliationService {
    @Autowired
    MemberReconciliationMapper memberReconciliationMapper;
    @Autowired
    TbBizOrderMemberReconciliationMapper tbBizOrderMemberReconciliationMapper;

    @Override
    public APIResponse<PageObj<MemberReconciliationVos>> getMemberReconciliation(ReqMemberReconciliation reqMemberReconciliation) {
        Integer pageNum = reqMemberReconciliation.getPageNum();
        Integer pageSize = reqMemberReconciliation.getPageSize();

        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 30;
        }
        reqMemberReconciliation.setPageStart((pageNum - 1) * pageSize);
        reqMemberReconciliation.setPageNum(pageNum);
        reqMemberReconciliation.setPageSize(pageSize);
        String name = reqMemberReconciliation.getName();
        String serialNo = reqMemberReconciliation.getSerialNo();
        String cardNo = reqMemberReconciliation.getCardNo();
        if (name != null)
            reqMemberReconciliation.setName(name.trim());
        if (serialNo != null)
            reqMemberReconciliation.setSerialNo(serialNo.trim());
        if (cardNo != null)
            reqMemberReconciliation.setCardNo(cardNo.trim());

        MemberReconciliationVos memberReconciliationVoss = new MemberReconciliationVos();
        List<MemberReconciliationVo> memberReconciliationVos = memberReconciliationMapper.getMemberReconciliation(reqMemberReconciliation);
        Map<String, Object> map = memberReconciliationMapper.getMemberReconciliationDifferenceAmount(reqMemberReconciliation);
        Double insertTotalAmount = MapUtils.getDouble(map, "insertTotalAmount");
        Double arrivalTotalAmount = MapUtils.getDouble(map, "arrivalTotalAmount");
        memberReconciliationVoss.setArrivalTotalAmount(arrivalTotalAmount == null ? new BigDecimal(0) : new BigDecimal(arrivalTotalAmount));
        memberReconciliationVoss.setInsertTotalAmount(insertTotalAmount == null ? new BigDecimal(0) : new BigDecimal(insertTotalAmount));
        memberReconciliationVoss.setDifferenceTotalAmount(memberReconciliationVoss.getArrivalTotalAmount().subtract(memberReconciliationVoss.getInsertTotalAmount()));
        memberReconciliationVoss.setMemberReconciliationVoList(memberReconciliationVos);
        int count = memberReconciliationMapper.memberReconciliationCount(reqMemberReconciliation);
        PageObj<MemberReconciliationVos> result = PageObj.create(count, pageNum, pageSize, memberReconciliationVoss);
        return APIResponse.returnSuccess(result);
    }

    @Override
    public APIResponse<MemberReconciliationVos> memberReconciliationAll(ReqMemberReconciliation reqMemberReconciliation) {
        MemberReconciliationVos memberReconciliationVoss = new MemberReconciliationVos();
        String name = reqMemberReconciliation.getName();
        String serialNo = reqMemberReconciliation.getSerialNo();
        String cardNo = reqMemberReconciliation.getCardNo();
        if (name != null)
            reqMemberReconciliation.setName(name.trim());
        if (serialNo != null)
            reqMemberReconciliation.setSerialNo(serialNo.trim());
        if (cardNo != null)
            reqMemberReconciliation.setCardNo(cardNo.trim());
        List<MemberReconciliationVo> memberReconciliationVos = memberReconciliationMapper.getMemberReconciliationAll(reqMemberReconciliation);
        Map<String, Object> map = memberReconciliationMapper.getMemberReconciliationDifferenceAmount(reqMemberReconciliation);
        Double insertTotalAmount = MapUtils.getDouble(map, "insertTotalAmount");
        Double arrivalTotalAmount = MapUtils.getDouble(map, "arrivalTotalAmount");
        memberReconciliationVoss.setArrivalTotalAmount(arrivalTotalAmount == null ? new BigDecimal(0) : new BigDecimal(arrivalTotalAmount));
        memberReconciliationVoss.setInsertTotalAmount(insertTotalAmount == null ? new BigDecimal(0) : new BigDecimal(insertTotalAmount));
        memberReconciliationVoss.setDifferenceTotalAmount(memberReconciliationVoss.getInsertTotalAmount().subtract(memberReconciliationVoss.getArrivalTotalAmount()));
        memberReconciliationVoss.setMemberReconciliationVoList(memberReconciliationVos);
        return APIResponse.returnSuccess(memberReconciliationVoss);
    }

    @Override
    public APIResponse<?> updateMemberStatus(ReqMemberReconciliationUpdate reqMemberReconciliationUpdate) {
        TbBizOrderMemberReconciliation tbBizOrderMemberReconciliation = new TbBizOrderMemberReconciliation();
        tbBizOrderMemberReconciliation.setOpUid(reqMemberReconciliationUpdate.getUserId());
        tbBizOrderMemberReconciliation.setOpUserName(reqMemberReconciliationUpdate.getUserName());
        tbBizOrderMemberReconciliation.setReconciliationDate(new Date());
        tbBizOrderMemberReconciliation.setRemark(reqMemberReconciliationUpdate.getRemark());
        tbBizOrderMemberReconciliation.setOpStatus((byte) 1);
        tbBizOrderMemberReconciliation.setIsDifference((byte) 1);
        TbBizOrderMemberReconciliationExample tbBizOrderMemberReconciliationExample = new TbBizOrderMemberReconciliationExample();
        tbBizOrderMemberReconciliationExample.createCriteria().andPlaceOrderIdEqualTo(reqMemberReconciliationUpdate.getMemberOrderNo()).andTypeEqualTo(reqMemberReconciliationUpdate.getType());
        int i = tbBizOrderMemberReconciliationMapper.updateByExampleSelective(tbBizOrderMemberReconciliation, tbBizOrderMemberReconciliationExample);
        if (i > 0) {
            return APIResponse.returnSuccess("跟新成功!");
        }
        return APIResponse.returnFail("跟新失败");
    }
}
